package com.sample.javabean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;




import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sample.action.form.ZhaoshengForm;
import com.sample.jdbc.JdbcUtils;
import com.sample.util.Pager;


/**
 * 幼儿信息类
 * @author梁桂花
 *
 * 2018-6-20
 */
public class Zhaosheng {
	
	private static Log LOG =LogFactory.getLog(Zhaosheng.class);
	//定义属性
	private Integer id;
	private String	name;
	private String	sex;
	private String	sno;
	private String	birthday;
	private String	className;
	private String	address;
	private String	tel;
	private String	pname;
	private String	cardno;
	
	private JdbcUtils jdbcUtils = new JdbcUtils();

	public Zhaosheng(Integer id){
		this.id =id;
	}
	
	/**
	 * zhaosheng信息列表
	 * @return
	 */
	public List<Zhaosheng> findAll(){
		Connection conn=null;
		Statement stmt =null;
		ResultSet rs =null;
		List<Zhaosheng> zhaoshengList =new ArrayList<Zhaosheng>(20);
		try{
			 conn =jdbcUtils.getConn();
			 stmt =conn.createStatement();
			//构造插入sql
			String sql="select * from t_zhaosheng where 1=1";
			 rs =stmt.executeQuery(sql);
			while(rs.next()){
				String name=rs.getString("name");
				String sex =rs.getString("sex");
			    String	sno=rs.getString("sno");
			    String	birthday=rs.getString("birthday");
			    String	className=rs.getString("className");
				String address=rs.getString("address");
				 String	tel=rs.getString("tel");
				 String	pname=rs.getString("pname");
				 String	cardno=rs.getString("cardno");	
				
				 Zhaosheng model=new Zhaosheng(id,name,sex,sno,birthday,className,address,tel,pname,cardno);
				
				 zhaoshengList.add(model);
							}
			
		}catch (Exception e) {
			e.printStackTrace();
			LOG.error("加载招生信息失败!", e);
		}finally{
			jdbcUtils.close(conn, stmt,rs);
		}
		return zhaoshengList;
		
	}
	
     public void load()throws Exception{
 		Connection conn = null;
 		Statement stmt = null;
 		ResultSet rs =null; 
 		try {
 			conn = jdbcUtils.getConn();
 			stmt = conn.createStatement();
 			
 			String sql = "select * from  t_zhaosheng where id='"+this.id+"'";
 		
 			rs=stmt.executeQuery(sql);
 			if(rs.next()){
 				this.id =rs.getInt("id");
 				this.name =rs.getString("name");
 				this.sex =rs.getString("sex");
 				this.sno=rs.getString("sno");
 			  	this.birthday=rs.getString("birthday");
 			   	this.className=rs.getString("className");
 				this.address=rs.getString("address");
 				this.tel=rs.getString("tel");
 				this.pname=rs.getString("pname");
 			   this.cardno=rs.getString("cardno");	
 				
 			}
 			// 5:閿熸埅鎲嬫嫹閿熸枻鎷锋簮
 		} catch (SQLException e) {
 			e.printStackTrace();
 			throw new SQLException("加载招生信息");
 		} finally {
 			jdbcUtils.close(conn, stmt);
 		}	
 	}
 	public void editZhaosheng() {

 		Connection conn = null;
 		Statement stmt = null;
 		try {
 			conn = jdbcUtils.getConn();
 			stmt = conn.createStatement();
 			
 			String sql = "update  t_zhaosheng set name='"+this.name+"',sex='"+this.sex+"',sno='"+this.sno+"',birthday='"+this.birthday+"',className='"+this.className+"',address='"+this.address+"',tel='"+this.tel+"',pname='"+this.pname+"',cardno='"+this.cardno+"' where id='"+this.id+"'";
 			
 			stmt.executeUpdate(sql);
 			// 5:閿熸埅鎲嬫嫹閿熸枻鎷锋簮
 		} catch (SQLException e) {
 			e.printStackTrace();
 		} finally {
 			jdbcUtils.close(conn, stmt);
 		}

 	}
 	public void delZhaosheng() {

 		Connection conn = null;
 		Statement stmt = null;
 		try {
 			conn = jdbcUtils.getConn();
 			stmt = conn.createStatement();
 			// 3:閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓绲奞L
 			String sql = "delete from t_zhaosheng  where sno='"+this.sno+"'";
 			// 4:鎵ч敓鏂ゆ嫹SQL閿熸枻鎷烽敓鏂ゆ嫹閿熸埅鏂ゆ嫹閿燂拷
 			System.out.println("删除招生信息");
 			stmt.executeUpdate(sql);
 			// 5:閿熸埅鎲嬫嫹閿熸枻鎷锋簮
 		} catch (SQLException e) {
 			e.printStackTrace();
 		} finally {
 			jdbcUtils.close(conn, stmt);
 		}

 	}
 	public void add(){
 		Connection conn =null;
 		Statement stmt=null;
 		try{
 			
 			 conn =jdbcUtils.getConn();
 		
 			String sql="insert into t_zhaosheng(name,sex,sno,birthday,className,address,tel,pname,cardno) values('"+this.name+"','"+this.sex+"','"+this.sno+"','"+this.birthday+"','"+this.className+"','"+this.address+"','"+this.tel+"','"+this.pname+"','"+this.cardno+"')";
 			
 			 stmt =conn.createStatement();
 			stmt.executeUpdate(sql);
 		}catch (Exception e) {
 			e.printStackTrace();
 			
 		}finally{
 			jdbcUtils.close(conn, stmt);
 			
 		}
 		
 	}
 	public Pager<Zhaosheng> list(int page,int size,ZhaoshengForm form) {

 		Connection conn = null;
 		Statement stmt = null;
 		ResultSet rs =null;
 		Pager<Zhaosheng> results=new Pager<Zhaosheng>();
 		results.setPage(page);
 		results.setSize(size);
 		try {
 			conn = jdbcUtils.getConn();
 			stmt = conn.createStatement();
 			int start=(page-1)*size;
 			int limit=size;
 			//閿熸枻鎷疯SQL
 			String selectSql="select * from t_zhaosheng where 1=1";
 			if(form!=null&&form.getSno()!=null&&!"".equals(form.getSno())){
 				selectSql +=" and sno like'%"+form.getSno()+"%'";
 			}
 			
 			//缁熼敓鏂ゆ嫹SQL
 			String countSql ="select count(*)as c from ("+selectSql+") as t1";
 			//閿熸枻鎷烽〉SQL
 			 String pageSql=selectSql+" limit "+start+","+limit;
 			 rs= stmt.executeQuery(countSql);
 			 int total=0;
 			 while(rs.next()){
 				 total=rs.getInt("c");
 			 }
 			 results.setTotal(total);
 			 //閿熸枻鎷疯閿熸枻鎷烽〉閿熸枻鎷锋伅
 			 rs= stmt.executeQuery(pageSql);
 			 while(rs.next()){
 				int id=rs.getInt("id");
 				String name=rs.getString("name");
				String sex =rs.getString("sex");
			    String	sno=rs.getString("sno");
			    String	birthday=rs.getString("birthday");
			    String	className=rs.getString("className");
				String	address	=rs.getString("address");
				 String	tel=rs.getString("tel");
				 String	pname=rs.getString("pname");
				 String	cardno=rs.getString("cardno");
 				 Zhaosheng model=new Zhaosheng();
 				 model.setId(id);
 				  model.setName(name);
 				  model.setSex(sex);
 				  model.setSno(sno);
 				  model.setBirthday(birthday);
 				  model.setClassName(className);
 				  model.setAddress(address);
 				  model.setTel(tel);
 				  model.setPname(pname);
 				  model.setCardno(cardno);
 				  
 		
 				  results.getDataSets().add(model);
 			 }
 			 results.init();
 			// 5:閿熸埅鎲嬫嫹閿熸枻鎷锋簮
 		} catch (SQLException e) {
 			e.printStackTrace();
 		} finally {
 			jdbcUtils.close(conn, stmt);
 		}
 		return results;

 	}
 	//定义无参构造函数
		public Zhaosheng(){
			
		}

	public Zhaosheng( Integer id,String name,String sex,String sno,String birthday,
			String className,String address	,String tel,String pname,String cardno ) {
		super();
		this.id = id;
		this.name = name;
		this.sex=sex;
		this.sno=sno;
		this.birthday=birthday;
		this.birthday=birthday;
		this.address=address;
	    this.tel=tel;
		this.pname=pname;
		this.cardno=cardno;
		
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getSno() {
		return sno;
	}

	public void setSno(String sno) {
		this.sno = sno;
	}

	public String getBirthday() {
		return birthday;
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getPname() {
		return pname;
	}

	public void setPname(String pname) {
		this.pname = pname;
	}

	public String getCardno() {
		return cardno;
	}

	public void setCardno(String cardno) {
		this.cardno = cardno;
	}

	public JdbcUtils getJdbcUtils() {
		return jdbcUtils;
	}

	public void setJdbcUtils(JdbcUtils jdbcUtils) {
		this.jdbcUtils = jdbcUtils;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	
}
